import random
from pypinyin import pinyin, Style
import tkinter as tk
from tkinter import messagebox, font

# 读取成语大全数据库
def load_idioms(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        idioms = file.readlines()
    return [idiom.strip() for idiom in idioms]

# 获取随机成语
def get_random_idiom(idioms):
    return random.choice(idioms)

# 判断成语是否在数据库中
def is_idiom_in_database(idiom, idioms):
    return idiom in idioms

# 获取机器接龙的成语
def get_machine_idiom(last_idiom, idioms):
    last_char_pinyin = pinyin(last_idiom[-1], style=Style.NORMAL)[0][0]
    available_idioms = [idiom for idiom in idioms if pinyin(idiom[0], style=Style.NORMAL)[0][0] == last_char_pinyin]

    if available_idioms:
        return random.choice(available_idioms)
    else:
        return None

# 获取提示
def get_hint(last_idiom, idioms):
    last_char_pinyin = pinyin(last_idiom[-1], style=Style.NORMAL)[0][0]
    hints = [idiom for idiom in idioms if pinyin(idiom[0], style=Style.NORMAL)[0][0] == last_char_pinyin]

    if hints:
        return random.choice(hints)
    else:
        return None

class IdiomSolitaireGame(tk.Tk):
    def __init__(self, idioms):
        super().__init__(useTk=True)  # 启用高DPI支持
        self.idioms = idioms
        self.current_idiom = get_random_idiom(idioms)
        self.title("成语接龙游戏")
        self.geometry("400x300")

        # 设置字体
        self.custom_font = font.Font(family="Arial", size=12)

        self.label = tk.Label(self, text=f"机器给出成语：{self.current_idiom}", font=self.custom_font)
        self.label.pack(pady=20)

        self.entry = tk.Entry(self, font=self.custom_font)
        self.entry.pack(pady=10)

        self.submit_button = tk.Button(self, text="提交", command=self.submit_idiom, font=self.custom_font)
        self.submit_button.pack(side=tk.LEFT, padx=20)

        self.hint_button = tk.Button(self, text="提示", command=self.show_hint, font=self.custom_font)
        self.hint_button.pack(side=tk.LEFT, padx=20)

        self.quit_button = tk.Button(self, text="结束", command=self.quit_game, font=self.custom_font)
        self.quit_button.pack(side=tk.LEFT, padx=20)

    def submit_idiom(self):
        user_input = self.entry.get().strip()
        if not user_input:
            self.label.config(text="请输入成语！")
            return

        last_char_pinyin = pinyin(self.current_idiom[-1], style=Style.NORMAL)[0][0]
        user_first_char_pinyin = pinyin(user_input[0], style=Style.NORMAL)[0][0]
        if user_first_char_pinyin != last_char_pinyin:
            self.label.config(text=f"成语接龙错误，应以'{self.current_idiom[-1]}'的拼音开头，请重新输入。")
            self.entry.delete(0, tk.END)
            return

        if not is_idiom_in_database(user_input, self.idioms):
            response = messagebox.askyesno("提示", "输入的成语不在数据库中，是否更换输入的成语？")
            if response:
                self.entry.delete(0, tk.END)
                return
            else:
                self.current_idiom = user_input
        else:
            self.label.config(text=f"用户给出成语：{user_input}")

        machine_idiom = get_machine_idiom(self.current_idiom, self.idioms)
        if machine_idiom:
            self.label.config(text=f"机器接龙成语：{machine_idiom}")
            self.current_idiom = machine_idiom
        else:
            self.label.config(text="机器无法接龙，游戏结束！")
            self.quit_game()
        self.entry.delete(0, tk.END)

    def show_hint(self):
        hint = get_hint(self.current_idiom, self.idioms)
        if hint:
            self.label.config(text=f"提示：{hint}")
        else:
            self.label.config(text="没有更多提示了，请继续尝试。")

    def quit_game(self):
        self.destroy()

if __name__ == "__main__":
    file_path = 'E:/成语大全.txt'
    idioms = load_idioms(file_path)
    print(f"成语库中有 {len(idioms)} 个成语。")
    game = IdiomSolitaireGame(idioms)
    game.mainloop()